<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.user_id" placeholder="用户ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.type" placeholder="类型" class="filter-item" @change="handleFilter">
        <el-option label="全部" value="" />
        <el-option label="充值" value="1" />
        <el-option label="聊天" value="2" />
        <el-option label="打赏" value="3" />
        <el-option label="收到打赏" value="4" />
        <el-option label="语音通话" value="5" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="用戶编号" align="center">
        <template slot-scope="{row}">
          {{ row.user_id }}
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;">{{ row.nickname }}</a>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="{row}">
          <span>{{ row.type | typeText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center">
        <template slot-scope="{row}">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金币" align="center">
        <template slot-scope="{row}">
          <span>{{ row.coins }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接受者ID" min-width="80" align="center">
        <template slot-scope="{row}">
          <span>{{ row.reception_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="礼物Id" align="center" min-width="80">
        <template slot-scope="{row}">
          <span>{{ row.gift_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单ID" align="center" min-width="80">
        <template slot-scope="{row}">
          <span>{{ row.order_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="created_at" sortable="custom" align="center" min-width="120">
        <template slot-scope="{row}">
          <span>{{ row.created_at | formatTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { coinsLogList } from '@/api/coinsLog'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves, elDragDialog },
  filters: {
    statusText(status) {
      const statusMap = {
        1: '未支付',
        2: '完成成功',
        3: '退款'
      }
      return statusMap[parseInt(status)]
    },
    typeText(status) {
      const statusMap = {
        1: '充值',
        2: '聊天',
        3: '打赏',
        4: '收到打赏',
        5: '语音通话'
      }
      return statusMap[parseInt(status)]
    },
    productsTypeText(status) {
      const statusMap = {
        1: '注册',
        2: '内购'
      }
      return statusMap[parseInt(status)]
    },
    formatTime: function(time) {
      if (parseInt(time) > 0) {
        return parseTime(parseInt(time), '{y}-{m}-{d} {h}:{i}')
      }
      return '-'
    }
  },
  props: {
    user_id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        status: '',
        sort: 'desc',
        sort_field: 'id',
        user_id: ''
      }
    }
  },
  watch: {
    user_id: {
      handler(newVal, oldVal) {
        this.listQuery.user_id = newVal
        this.getList()
      }
    }
  },
  created() {
    this.listQuery.user_id = this.user_id
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      coinsLogList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      this.listQuery.sort_field = data.prop
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      this.handleFilter()
    },
    getSortClass: function() {
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      return this.listQuery.sort === 'asc' ? 'ascending' : 'descending'
    }
  }
}
</script>

<style>
.click_a {
  color: #409EFF;
  padding-right: 5px;
}
</style>
